//
//  SISSContractTextChooseCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/1/8.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSContractTextChooseCell.h"
#import "SISSPickerView.h"

@interface SISSContractTextChooseCell()
/// label
@property (weak, nonatomic) YYLabel *label;

/// textFieldArrM
@property (weak, nonatomic) UIButton *chooseButton;

@property (strong, nonatomic) NSMutableArray *arrM;
/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;

@end
@implementation SISSContractTextChooseCell


- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    //    self.bottomLine.hidden = contractModel.isHiddenBottomLine;
    if (!contractModel.strArr) return;
   
   
    NSMutableArray *strArrM = [NSMutableArray arrayWithArray:contractModel.strArr];
    [strArrM removeObjectAtIndex:0];
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:contractModel.strArr.firstObject attributes:nil];
    
    UIFont *font = [UIFont systemFontOfSize:16];
    
    NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:self.chooseButton contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(88, 3) alignToFont:font alignment:(YYTextVerticalAlignment)YYTextVerticalAlignmentCenter];
    [attributedText appendAttributedString:attachText];
    [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:contractModel.strArr.lastObject attributes:nil]];
    attributedText.yy_color = SECONDARY_TEXT_COLOR;
    attributedText.yy_font = font;
    if ([[contractModel.strArr.firstObject substringToIndex:1] isEqualToString:@"*"]) {
        [attachText setAttributes:@{
                              NSForegroundColorAttributeName : [UIColor redColor],
                              NSFontAttributeName : [UIFont systemFontOfSize:16]
                              } range:NSMakeRange(0, 1)];
        
    }
    self.label.attributedText = attributedText;
}

- (void)buttonClickAction:(UIButton *)button {
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    NSInteger length = 0;
    NSInteger location = 0;
    if ([self.contractModel.leftStr hasPrefix:@"*"]) {
        length += 1;
        location = 1;
    }
    if ([self.contractModel.leftStr hasSuffix:@"："]) {
        length += 1;
    }
    
    pickerView.title = [self.contractModel.leftStr substringWithRange:NSMakeRange(location, self.contractModel.leftStr.length - length)];
    BOOL isArea = self.contractModel.isArea;
    pickerView.isArea = isArea;
    NSMutableArray *chooseArr = [NSMutableArray arrayWithCapacity:100];
    for (NSInteger i = 0; i < 100; i++) {
        [chooseArr addObject:[NSString stringWithFormat:@"%zd%%",i]];
    }
    pickerView.titleArray = chooseArr; // self.contractModel.chooseArr;
    pickerView.firstIndex = self.contractModel.firstIndex;
    //    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
//        self.rightTextField.text = firstTitle;
        [self.chooseButton setTitle:firstTitle forState:UIControlStateNormal];
        self.contractModel.rightStr = firstTitle;
//        if ([self.delegate respondsToSelector:@selector(contractChooseCell:chooseIndex:)]) {
//            [self.delegate contractChooseCell:self chooseIndex:firstIndex];
//        }
        self.contractModel.firstIndex = firstIndex;
        
    };
    
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
}


#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    YYLabel *label = [[YYLabel alloc] init];
    label.numberOfLines = 0;
    label.preferredMaxLayoutWidth = (SCREEN_WIDTH - 32);
    label.userInteractionEnabled= YES;
    [self.contentView addSubview:label];
    self.label = label;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 8, 88, 32);
    [button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    self.chooseButton = button;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.contentView).offset(2 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.label.mas_bottom).offset(margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}



- (NSMutableAttributedString *)getTextAttritedStrWithAttachText:(NSMutableAttributedString *)attachTextM strs:(NSArray *)strArr textFields:(NSArray <UITextField *>*)textFields placeHolder:(NSString *)placeHolder {
    
    UIFont *font = [UIFont systemFontOfSize:16];
    for (NSInteger i = 0; i < textFields.count; i++) {
        UITextField *textField = textFields[i];
//        textField.delegate = self;
        textField.textColor = MAIN_TEXT_COLOR;
        textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        textField.placeholder = placeHolder ? [NSString stringWithFormat:@"%@   ",placeHolder] : @" 几     ";
        //        textField.textAlignment = NSTextAlignmentCenter;
        //        textField.userInteractionEnabled = NO;
        [textField sizeToFit];
        UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(10,0,10,26)];
        leftView.backgroundColor = [UIColor clearColor];
        textField.leftView = leftView;
        textField.leftViewMode = UITextFieldViewModeAlways;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        
        NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:textField contentMode:UIViewContentModeCenter attachmentSize:textField.bounds.size alignToFont:font alignment:(YYTextVerticalAlignment)YYTextVerticalAlignmentCenter];
        [attachTextM appendAttributedString:attachText];
        [attachTextM appendAttributedString:[[NSAttributedString alloc] initWithString:strArr[i] attributes:nil]];
    }
    attachTextM.yy_color = SECONDARY_TEXT_COLOR;
    attachTextM.yy_font = font;
    return attachTextM;
}

@end
